//
//  KDNewfeatureController.m
//  警用舆情平台
//
//  Created by 魏强 on 15/5/12.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDNewfeatureController.h"
#import "KDNavigationController.h"
#import "KDInspectController.h"
#import "KDLoginResult.h"
#import "KDLoginParam.h"
#import "KDNewFeatureRequestTool.h"
#import "KDAccount.h"
#import "KDAccountTool.h"
#import "KDLoginTextField.h"
#import "KDLoginButton.h"
#import "KDVpnDataTool.h"
#import "KDRole.h"
#import "SVProgressHUD.h"
#import <CommonCrypto/CommonDigest.h>
#import "AFNetworking.h"
#import "KDTabBarController.h"
#import <sys/utsname.h>
#import "WQMqttManager.h"
#import "KDLoginTool.h"
#import "KDUserInfo.h"
#define BTN_TAG 333
#define TEXTFIELD_FONT [UIFont systemFontOfSize:11.5]
#define CHOOSEVIEW_W 200
#define SEGMENT_H 30
#define VERSION @"version"
#define BTN_WIDTH CHOOSEVIEW_W / 2

@interface KDNewfeatureController ()<UITextFieldDelegate>
{
    UIView *loginview; //加载登陆界面
    UIScrollView *scrollview; //加载切图界面
    KDLoginTextField *userNameField; //用户名输入框
    KDLoginTextField *passWordField; //密码输入框
    UIImageView *userNameImage; //用户名图片
    UIImageView *passWordImage; //密码图片
    KDLoginButton *clickInButton; //登录按钮
    NSString *logIn; //登陆url
    UILabel *titleLabel; //登陆的标签
//    UISegmentedControl *segment;
}
/**
 *  版本选择View
 */
@property (nonatomic, weak) UIView *chooseView;
/***  被选中的主题btn*/
//@property (nonatomic, strong) UIButton *selectBtn;
/**
 *  头部标题下面的红线
 */
@property (nonatomic, strong) UIView *redLine;
@end

@implementation KDNewfeatureController
/***  设置selectBtn的*/
//- (void)setSelectBtn:(UIButton *)selectBtn
//{
//    _selectBtn = selectBtn;
//    _selectBtn.selected = YES;
//}
//- (UIView *)redLine{
//    if (!_redLine) {
//        _redLine = [[UIView alloc]initWithFrame:CGRectMake(10, self.chooseView.height -2, BTN_WIDTH -20, 2)];
//        [self.chooseView addSubview:_redLine];
//        _redLine.backgroundColor = KDHeadColor;
//    }
//    [self.chooseView bringSubviewToFront:_redLine];
//    return _redLine;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    dispatch_queue_t queue;
    queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
    dispatch_async(queue, ^{
        [[WQMqttManager sharedWQMqttManager] disconnect];
        
    });
    KDLog(@"%s", __func__);
    NSUserDefaults *userDefaults4 = [NSUserDefaults standardUserDefaults];
    //保存数据
    [userDefaults4 setObject:@"NO" forKey:@"Login"];
    [userDefaults4 setObject:@(0) forKey:VERSION];
    //同步数据
    [userDefaults4 synchronize];

    NSString *key = @"CFBundleShortVersionString";
    // 上一次的使用版本（存储在沙盒中的版本号）
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    // 当前软件的版本号（从Info.plist中获得）
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    
    if ([currentVersion isEqualToString:lastVersion])
    {
        // 版本号相同：这次打开和上次打开的是同一个版本
        [self createLoginView];
        self.view.backgroundColor = [UIColor whiteColor];
        
    }
    else
    {
        // 这次打开的版本和上一次不一样，显示新特性
        [self creatTheScrollView];
        // 将当前的版本号存进沙盒
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)creatTheScrollView
{
    scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    scrollview.contentSize = CGSizeMake(screenW * 4, screenH);
    //KDLog(@"%f",self.view.frame.size.height);
    //水平滚动条
    scrollview.showsHorizontalScrollIndicator = NO;
    //垂直滚动条
    scrollview.showsVerticalScrollIndicator = NO;
    //设置翻页效果
    scrollview.pagingEnabled = YES;
    //创建三个切图界面
    UIImage *image1;
    UIImage *image2;
    UIImage *image3;
    UIImage *image4;
    image1 = [UIImage imageNamed:@"引导001"];
    image2 = [UIImage imageNamed:@"引导002"];
    image3 = [UIImage imageNamed:@"引导003"];
    image4 = [UIImage imageNamed:@"引导004"];
    
    if (IS_PAD) {
        image1 = [UIImage imageNamed:@"引导401"];
        image2 = [UIImage imageNamed:@"引导402"];
        image3 = [UIImage imageNamed:@"引导403"];
        image4 = [UIImage imageNamed:@"引导404"];
    }
    
//    KDLog(@"%@",NSStringFromCGSize([[UIScreen mainScreen] applicationFrame].size));
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    UIImageView *imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    imageview1.image = image1;

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(screenW, 0, screenW, screenH)];
    UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    imageview2.image = image2;
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(screenW * 2, 0, screenW, screenH)];
    UIImageView *imageview3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    imageview3.image = image3;
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(screenW * 3, 0, screenW, screenH)];
    UIImageView *imageview4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    imageview4.image = image4;
    
    //设置按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(90, screenH - 120, 150, 40);
    btn.y = screenH * 0.88;
    btn.center = CGPointMake(screenW * 0.5, btn.centerY);
    [btn setImage:[UIImage imageNamed:@"立即体验"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(createLoginView) forControlEvents:UIControlEventTouchUpInside];
    btn.layer.cornerRadius = 8;
    if (screenH == 480) {
        CGPoint point = CGPointMake(screenW *0.5, btn.centerY);
        btn.width = btn.width * 0.8;
        btn.height = btn.height *0.8;
        btn.center = point;
    }
    [view1 addSubview:imageview1];
    [view2 addSubview:imageview2];
    [view3 addSubview:imageview3];
    [view4 addSubview:imageview4];
    [view4 addSubview:btn];
    
    [scrollview addSubview:view1];
    [scrollview addSubview:view2];
    [scrollview addSubview:view3];
    [scrollview addSubview:view4];
    [self.view addSubview:scrollview];
    //[self.view addSubview:pageControl];
}

-(void)createLoginView
{
    //释放scrollview
    [scrollview removeFromSuperview];
    loginview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 20)];
    statusBarView.backgroundColor=KDColor;
//    
//    titleLabel.font = [UIFont boldSystemFontOfSize:19];
//    titleLabel.textColor = [UIColor whiteColor];
    
    [self setUpChooseView];
    
    //创建输入框
    userNameField = [[KDLoginTextField alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.chooseView.frame) + 30, screenW , 45)];
    passWordField = [[KDLoginTextField alloc] initWithFrame:CGRectMake(userNameField.x, CGRectGetMaxY(userNameField.frame) + 1, userNameField.width, userNameField.height)];
    userNameField.delegate = self;
    userNameField.tfName = @"用户名";
    passWordField.tfName = @"密码";
    passWordField.delegate = self;
//    userNameField.borderStyle = UITextBorderStyleRoundedRect;
//    passWordField.borderStyle = UITextBorderStyleRoundedRect;
    
    userNameField.placeholder = @"请输入账号";
    userNameField.font = [UIFont systemFontOfSize:11.5];
    userNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameField.clearsOnBeginEditing = YES;
    UIColor *color = WQColor(161, 161, 161);
    userNameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:@{NSForegroundColorAttributeName: color}];
    
    passWordField.secureTextEntry = YES;
    passWordField.placeholder = @"请输入密码";
    UIColor *color1 = WQColor(161, 161, 161);
    passWordField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName: color1}];
    passWordField.font = [UIFont systemFontOfSize:11.5];
    
    //生成输入框的图片
    userNameImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 35, 38)];
    UIImage  *userName = [UIImage imageNamed:@"登陆页名称"];
    userNameImage.image = userName;
    passWordImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 35, 38)];
    UIImage  *passWord = [UIImage imageNamed:@"登陆页密码"];
    passWordImage.image = passWord;

    userNameImage.size = userName.size;
    passWordImage.size = passWord.size;
    userNameField.leftView = userNameImage;
    passWordField.leftView = passWordImage;
    passWordField.leftView.contentMode = UIViewContentModeCenter;
    userNameField.leftViewMode = UITextFieldViewModeAlways;
    passWordField.leftViewMode = UITextFieldViewModeAlways;
    
    clickInButton = [KDLoginButton buttonWithType:UIButtonTypeCustom];
    clickInButton.frame = CGRectMake(15,  CGRectGetMaxY(passWordField.frame) + 25, screenW - 30, 30);

    [clickInButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    //添加手势
    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] init];
    [hide addTarget:self action:@selector(hideTheKeyBoard)];
    loginview.backgroundColor = BackGroundColor;
    [loginview addSubview:titleLabel];
    [loginview addSubview:userNameField];
    [loginview addSubview:passWordField];
    [loginview addSubview:clickInButton];
    [loginview addGestureRecognizer:hide];
    [loginview addSubview:statusBarView];
    [self.view addSubview:loginview];
    KDAccount *account = [KDAccountTool account];
    if (account.userName) {
        userNameField.text = account.loginName;
        passWordField.text = account.password;
    }
}

- (void)setUpChooseView{
    UIView *chooseView = [[UIView alloc]init];
    self.chooseView = chooseView;
    chooseView.backgroundColor = [UIColor clearColor];
    chooseView.x = 0;
    chooseView.y = 20;
    chooseView.size = CGSizeMake(screenW, 44);
//    NSArray *versions = @[@"平台版", @"非平台版"];
    UILabel *titleLb = [[UILabel alloc]init];
    titleLb.text = @"登录";
    titleLb.textColor = [UIColor whiteColor];
    titleLb.backgroundColor = KDColor;
    titleLb.frame = chooseView.bounds;
    titleLb.textAlignment = NSTextAlignmentCenter;
    [chooseView addSubview:titleLb];
//    }
    [loginview addSubview:chooseView];
    
}

- (void)segmentClick:(UISegmentedControl *)segment1{
    //保存数据
    [[NSUserDefaults standardUserDefaults] setObject:@(segment1.selectedSegmentIndex) forKey:VERSION];
    //同步数据
    [[NSUserDefaults standardUserDefaults] synchronize];
}
//设置状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
//- (void)setNeedsStatusBarAppearanceUpdate{
//    [self preferredStatusBarStyle];
//}
#pragma mark - 隐藏键盘
-(void)hideTheKeyBoard
{
    [loginview endEditing:YES];
}
#pragma mark - 将密码MD5转码两次
-(NSString *)MD5password:(NSString *)password
{
    const char *cStr = [password UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (uint32_t)strlen(cStr), result ); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
#pragma mark - 请求服务器检查用户名与密码的正确
-(void)login
{
    if(userNameField.text.length == 0)
    {
        [userNameField TextFieldShake];
        return;
    }else if(passWordField.text.length == 0)
    {
        [passWordField TextFieldShake];
        
        return;
    }
    [clickInButton startCircle];
        //创建param
    KDLoginParam *param = [[KDLoginParam alloc]init];
    
    //NSUserDefaults
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //读取数据
//    NSString *str = [userDefaults stringForKey:UUID];
//    param.token = str;
    param.loginName = userNameField.text ;
    param.loginPwd = passWordField.text;
//    param.subs_pwd = [self MD5password:passWordField.text];
    KDAccount *account = [KDAccountTool account];
    param.token = account.deviceToken;
    param.system = @"S04SU02";
    param.validCode = @"string";
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSMutableDictionary *parameters = param.keyValues;
    param.method = @"user/login";
    NSString *url = [NSString stringWithFormat:@"%@%@",AuthServerAddr,param.method];
    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:nil error:nil];
    NSError *error1;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
                                                       options:0
                                                         error:&error1];
//    req.timeoutInterval= 30;
    [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [req setHTTPBody:jsonData];
    __weak __typeof(self)weakSelf = self;
    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        if (!error) {
            KDLoginResult *result = [KDLoginResult objectWithKeyValues:responseObject];
            KDLog(@"%@",responseObject);
            if ([result.msg isEqualToString:@"操作成功"]) {
                KDAccount *account = [[KDAccount alloc]init];
                account.token = result.data;
//                KDLog(@"token= %@\ntokens= %@",account.token, account.tokens);
                account.password = passWordField.text;

                account.userName = param.loginName;
                [KDAccountTool saveAccount:account];
                //获取用户信息保存
                [weakSelf getUserInfo];
                NSUserDefaults *userDefaults4 = [NSUserDefaults standardUserDefaults];
                //保存数据
                [userDefaults4 setObject:@"YES" forKey:@"Login"];
                //同步数据
                [userDefaults4 synchronize];
                
                //根据不同的integer值跳转到主页面
                [self goToMain];
                //account.
            }else{
                [SVProgressHUD showErrorWithStatus:result.msg maskType:SVProgressHUDMaskTypeGradient];
                [self stopCircle];
            }

        } else {
            [SVProgressHUD showErrorWithStatus:@"登陆失败,请检查网络信息！" maskType:SVProgressHUDMaskTypeGradient];
            [self stopCircle];
        }
    }] resume];

}

- (void)getUserInfo{
    KDBaseParam *param = [[KDBaseParam alloc]init];
    param.method = @"users/userInfo";
    [KDLoginTool getUserInfoWithParam:param success:^(KDBaseResult *responseObejct) {
       KDAccount *userInfo = [KDAccount objectWithKeyValues:responseObejct.data];
        
        KDAccount *account = [KDAccountTool account];
        userInfo.token = account.token;
        
        [KDAccountTool saveAccount:userInfo];
        
    } failure:^(NSError *error) {
        KDLog(@"%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络错误！" maskType:SVProgressHUDMaskTypeGradient];
    }];
}
- (NSString *)ua{
    NSString *ua = nil;
    //版本号
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    //手机系统平台
    NSString *system = @"iOS";
    //手机系统版本
    NSString *systemVersion = [NSString stringWithFormat:@"%f", [[UIDevice currentDevice].systemVersion doubleValue]];
    //手机类型
    NSString* phoneModel = [self iphoneType];
    //手机惟一标识
    //    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //读取数据
    NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:UUID];
    ua = [NSString stringWithFormat:@"%@/%@/%@/%@/%@/%@",appVersion,system,systemVersion,phoneModel,uuid,nil];
    return ua;
}
- (NSString *)iphoneType {
        
    struct utsname systemInfo;
    
    uname(&systemInfo);
    
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
    
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    
    if ([platform isEqualToString:@"iPod1,1"])   return @"iPod Touch 1G";
    
    if ([platform isEqualToString:@"iPod2,1"])   return @"iPod Touch 2G";
    
    if ([platform isEqualToString:@"iPod3,1"])   return @"iPod Touch 3G";
    
    if ([platform isEqualToString:@"iPod4,1"])   return @"iPod Touch 4G";
    
    if ([platform isEqualToString:@"iPod5,1"])   return @"iPod Touch 5G";
    
    if ([platform isEqualToString:@"iPad1,1"])   return @"iPad 1G";
    
    if ([platform isEqualToString:@"iPad2,1"])   return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,2"])   return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,3"])   return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,4"])   return @"iPad 2";
    
    if ([platform isEqualToString:@"iPad2,5"])   return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad2,6"])   return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad2,7"])   return @"iPad Mini 1G";
    
    if ([platform isEqualToString:@"iPad3,1"])   return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,2"])   return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,3"])   return @"iPad 3";
    
    if ([platform isEqualToString:@"iPad3,4"])   return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad3,5"])   return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad3,6"])   return @"iPad 4";
    
    if ([platform isEqualToString:@"iPad4,1"])   return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,2"])   return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,3"])   return @"iPad Air";
    
    if ([platform isEqualToString:@"iPad4,4"])   return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"iPad4,5"])   return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"iPad4,6"])   return @"iPad Mini 2G";
    
    if ([platform isEqualToString:@"i386"])      return @"iPhone Simulator";
    
    if ([platform isEqualToString:@"x86_64"])    return @"iPhone Simulator";
    
    return platform;
    
}

- (void)stopCircle{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [clickInButton stopCircle];
    });
}
#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - 跳转到主页面
-(void)goToMain
{
    [UIView animateWithDuration:3.58 animations:^{
        [self stopCircle];
    }completion:^(BOOL finished) {
        //添加跳转动画
        CATransition *ca = [CATransition animation];
        //动画持续时间
        ca.duration = 1.58;
        //设置动画的曲线方式（就是动画的总体变化的时间曲线：开始快最后慢，开始慢最后快，最后慢，均匀线性）
        ca.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//        ca.type = @"rippleEffect";
        [loginview removeFromSuperview];
//        KDInspectController *tabCtrl = [[KDInspectController alloc]init];
//        [self presentViewController:tabCtrl animated:YES completion:nil];
//        [self.view.window.layer addAnimation:ca forKey:nil];
//        segment.tintColor = KDHeadColor;
//        UIWindow *win =[UIApplication sharedApplication].keyWindow;
        
        [UIApplication sharedApplication].keyWindow.rootViewController = [[KDTabBarController alloc]init];
    }];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - 弹出的对话框内容
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert = nil;
}
@end
